package com.qiyun.duijiang.utils;

public class Zcutil {
    // 求排列数 A(n,m) n>m
    public static int A(int n, int m) {
        int result = 1;
        // 循环m次,如A(6,2)需要循环2次，6*5
        for (int i = m; i > 0; i--) {
            result *= n;
            n--;// 下一次减一
        }
        return result;
    }
    // 应用组合数的互补率简化计算量
    public static int C(int n, int m) {
        int helf = n / 2;
        if (m > helf) {
            m = n - m;
        }
        // 分子的排列数
        int numerator = A(n, m);
        // 分母的排列数
        int denominator = A(m, m);
        return numerator / denominator;
    }

    public static int ren9(String s) {
        //String s = "310,310,310,310,310,310,310,310,310,310,310,310,310,310";
        String[] arr = s.split(",");
        int num = 0;
        int one = arr.length;
        if (one<9) return num;
        int two = 0;
        for(int o=0;o<arr.length;o++){
            if (arr[o].length()>1) two+=1;
        }
        if(two==0) {
            num = C(one, 9);
            return num;
        }
        int three = 0;
        for(int p=0;p<arr.length;p++) {
            if (arr[p].length() > 2) three += 1;
        }
        int m = two;
        if (three == 0) {
            if (m > 9) m = 9;
            for (int i = 0; i <= m; i++) {
                num = num + C(two, i) * C(one - i, 9 - i);
            }
        } else {
            for (int i = 0; i <= three; i++) {
                int count = 0;
                if (m > 9) m = 9;
                for (int j = 0; j <= m - i; j++) {
                    count = count + C(two - i, j) * C(one - (j + i), 9 - (j + i));
                }
                num = num + C(three, i) * count;
            }
        }
        return num;
    }
}
